package ve.com.wayoyo.wuml_case.DibujoDiagramas;

import java.util.ArrayList;

import ve.com.wayoyo.wuml_case.UserPref;
import android.annotation.SuppressLint;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Environment;

@SuppressLint("ParserError")
public class CompClase extends Componente {

	private Paint _borderPaint;
	private Paint _textPaint;
	private Paint _focusPaint;
	private Paint _connectingPaint;
	
	private float _width = 0;
	private float _height = 80;
	
	private ArrayList<CompAtributo> _atributos = new ArrayList<CompAtributo>();
	private ArrayList<CompMetodo> _metodos = new ArrayList<CompMetodo>();
	
	public CompClase(String Titulo){
		if(UserPref.getInstancia().isUsingConvections() && Titulo.length() > 1){
			this.Titulo = Titulo.substring(0,1).toUpperCase() + Titulo.substring(1);
		}else{
			this.Titulo =  Titulo;
		}
		_borderPaint = new Paint();
		_borderPaint.setColor(Color.BLACK);
		_borderPaint.setStrokeWidth(3);
		_borderPaint.setAntiAlias(false);
		_textPaint = new Paint();
		_textPaint.setColor(Color.BLACK);
		_textPaint.setTextSize(25);
		_focusPaint = new Paint();
		_focusPaint.setColor(Color.GREEN);
		_focusPaint.setStrokeWidth(4);
		_connectingPaint = new Paint();
		_connectingPaint.setColor(Color.GREEN);
		_connectingPaint.setStrokeWidth(4);
		Point punto = new Point();
		punto.x = 20;
		punto.y = 20;
		this.setAnchor(punto);
		_width = _textPaint.measureText(Titulo)+15;
		
		CompMetodo a = new CompMetodo("HacerAlgo","public");
		a.addVariable(new CompVariable("Var1","int"));
		a.setTipoDato("void");
		CompMetodo b = new CompMetodo("HacerOtraCosa","private");
		//b.addVariable(new CompVariable("Var1","int"));
		b.setTipoDato("int");
		addMetodo(a);
		addMetodo(b);
	}
	
	@Override
	public void Dibujar(Canvas c,boolean isFcoused,float globalScale,int offSetX,int offSetY) {
		int posX = Anchor.x + offSetX;
		int posY = Anchor.y + offSetY;
		_textPaint.setTextSize(25*globalScale);				
		//Dibujamos el Cuadro
		if(isFocus){
			c.drawLine(posX*globalScale,posY*globalScale,(posX+_width)*globalScale,posY*globalScale,_focusPaint);
			c.drawLine(posX*globalScale,posY*globalScale,posX*globalScale,(posY+_height)*globalScale,_focusPaint);
			c.drawLine(posX*globalScale,(posY+_height)*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_focusPaint);
			c.drawLine((posX+_width)*globalScale,posY*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_focusPaint);
		
		}else{
			c.drawLine(posX*globalScale,posY*globalScale,(posX+_width)*globalScale,posY*globalScale,_borderPaint);
			c.drawLine(posX*globalScale,posY*globalScale,posX*globalScale,(posY+_height)*globalScale,_borderPaint);
			c.drawLine(posX*globalScale,(posY+_height)*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_borderPaint);
			c.drawLine((posX+_width)*globalScale,posY*globalScale,(posX+_width)*globalScale,(posY+_height)*globalScale,_borderPaint);
		}
		
		//Dibujamos la linea del titulo
		c.drawLine(posX*globalScale,(posY+30)*globalScale,(posX+_width)*globalScale,(posY+30)*globalScale,_borderPaint);
		
		//Dibujamos el titulo
		c.drawText(Titulo, (posX+5)*globalScale ,(posY+25)*globalScale , _textPaint);
		
		//Dibujamos los atributos
		int puntoEscritura = 0;
		for (CompAtributo value : _atributos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX+5)*globalScale ,(posY+30+puntoEscritura)*globalScale , _textPaint);
		}
		//Dibujamos la linea q separa metodos y atributos
		c.drawLine(posX*globalScale,(posY+45+puntoEscritura)*globalScale,(posX+_width)*globalScale,(posY+45+puntoEscritura)*globalScale,_borderPaint);
		puntoEscritura += 10;
		
		//Dibujamos los metodos
		for (CompMetodo value : _metodos) {
			puntoEscritura += 30;
			c.drawText(value.getText(), (posX+5)*globalScale ,(posY+30+puntoEscritura)*globalScale , _textPaint);
		}
		
		//Dibujamos Los conectores Padre
		for (CompConector value : _conectorsBegining) {
			value.Dibujar(c, isFcoused, globalScale, offSetX, offSetY);
		}
	}

	@Override
	public boolean CalcularVisibilidad(int offSetX, int offSetY,float GlobalScale) {
		boolean Visible = true;
		if((Anchor.x + _width) <= offSetX){
			Visible = false;
		}else if(Anchor.x <= offSetX + UserPref.getInstancia().getWinWidth()){
			Visible = false;
		}else if(Anchor.y > offSetY + UserPref.getInstancia().getWinHeight()){
			Visible = false;
		}else if((Anchor.y + _height) <= offSetY){
			Visible = false;
		}
		return Visible;
	}

	@Override
	public boolean calcularTouch(Point touch) {
		boolean respuesta = false;
		if((touch.x <= Anchor.x+_width) && (touch.x >= Anchor.x)){
			if((touch.y <= Anchor.y+_height) && (touch.y >= Anchor.y)){
				isFocus = true;
				respuesta = true;
				//FactoryPopup.getPopup(VistaDiagrama.getVista(), "Clases");
			}
		}
		return respuesta;
	}

	public void addAtributo(CompAtributo nuevo){
		_atributos.add(nuevo);
		if(_width-10 < _textPaint.measureText(nuevo.getText()) ){
			_width = _textPaint.measureText(nuevo.getText())+10;
		}
		_height += 30;
	}
	
	public void addMetodo(CompMetodo nuevo){
		_metodos.add(nuevo);
		if(_width-10 < _textPaint.measureText(nuevo.getText()) ){
			_width = _textPaint.measureText(nuevo.getText())+10;
		}
		_height += 30;
	}

	@Override
	public void setTitulo(String titulo) {
		if(UserPref.getInstancia().isUsingConvections()){
			this.Titulo = titulo.substring(0,1).toUpperCase() + titulo.substring(1);
			if(_width-10 < _textPaint.measureText(Titulo) ){
				_width = _textPaint.measureText(Titulo)+10;
			}
		}else{
			this.Titulo =  titulo;
		}
	}

	@Override
	public boolean calcularTouchConectores(Point touch) {
		
		return false;
	}
	
	public ArrayList<CompAtributo> getListaAtributo() {
		return _atributos;		
	}
}
